package com.friendar.webapp;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class for Servlet: LocationUpdate
 * 
 */
public class LocationUpdate extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public LocationUpdate() {
		super();
	}

	/*
	 * This method is called with couple of query parameters to register the
	 * location of a user The arguments passed will be id of user, lat and long
	 * co-ordinates retrieved from GPS This will be stored in the database and
	 * then matched with other friends. If it matches other friends, it will be
	 * stored. Client device will poll another servlet (FriendsFinder) to get
	 * the information.
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		// Step 1: Process the query parameters
		String userID, lat, longi;
		boolean respOk = false;
		
		// set the content type to XML before anything is written back
		response.setContentType("application/xml");
		
		userID = request.getParameter("uid");
		if ((userID == null) || (userID.isEmpty())) {
			// TODO add code to handle empty USER ID
		}
		lat = request.getParameter("lat");
		if ((lat == null) || (lat.isEmpty())) {
			// TODO add code to handle empty latitude
		}

		longi = request.getParameter("longi");
		if ((longi == null) || (longi.isEmpty())) {
			// TODO add code to handle empty longitude
			// TODO add format checks for lat and long values
		}

		// Step 2: Check if the user is registered with the service
		ProfileHandler ph = ProfileHandler.getInstance();
		if (ph.isConnected()) {
			System.out.println("Connection established");
			if (ph.isValidUser(userID)) {
				// Step 3: Update the DB with the info
				if (ph.updateLocation(userID, lat, longi)) {
					// the update was successful
					respOk = true;
				} 
			}
		}
		if(respOk) {
			response
			.getWriter()
			.write(
					"<response> <action>Update Location</action><status>OK</status></response>");
		} else {
			response
			.getWriter()
			.write(
					"<response> <action>Update Location</action><status>Error</status></response>");
		}
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.GenericServlet#init()
	 */
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
	}
}